package stars.starSystems;

import java.util.ArrayList;
import java.util.Random;

import stars.enums.StarSystemDistributions;

/*
 * Given list of star system types and probabilities, chooses a system type
 */
public class StarSystemLayoutBuilder {
	
	Random rand = new Random();
	
	public String chooseLayout(StarSystemDistributions sysDists) {
		
		double value = rand.nextDouble();
		
		ArrayList<String> types = sysDists.getEntries();
		
		//already normalized, so commented this out
//		double cumuProb = 0.0;
//		
//		//Normalize data first
//		for (String type: types) {
//			cumuProb+=sysDists.getProbability(type);
//		}
//		
//		double correction = 1;
//		
//		if (cumuProb != 1.0) {
//			correction = 1/cumuProb;
//		}
		
		
		double cumuProb = 0.0;
		double typeProb = 0.0;
		String lastType = types.get(0);
		for (String type: types) {
			typeProb = sysDists.getProbability(type);
			cumuProb += typeProb;
			if (cumuProb > value) {
				return type;
			}
			else {
				lastType = type;
			}			
		}
		
		return lastType; //in case the type probabilities don't equal 1, algorithm returns the last type examined
	}

}
